;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;       Desc: Lib used by pm32 code
;       copyright (c) 2014 -
;       BrooksGe
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        [section        .text]
        [bits           32]
        align           16
        global          _enable_longMode  
        ;
        ;void enable_longMode(void * Pml4Addr, void * entry, UINT32 selector);
        ;
_enable_longMode:
        ;
        ;Because never ret from this function,
        ;so we didn't save the regiseter.
        ;enable the long mode and jmp to.
        ;
        push    ebp
        mov     ebp, esp
        ;setting CR4.PAE=1
        mov     eax, cr4
        bts     eax, 5
        mov     cr4, eax
        ;init the long-mode page tables
        mov     eax, [ebp + 8]
        mov     cr3, eax
        ;Enable long mode (set EFER.LME=1)
        mov     ecx, 0c0000080h ;EFER MSR number.
        rdmsr
        bts     eax, 8
        wrmsr
        ;Enable paging to activate long mode (set CR0.PG=1)
        mov     eax, cr0
        bts     eax, 31
        mov     cr0, eax
        jmp     $ + 2
        ;jmp to 64 bits long mode
        leave
        add     esp, 8
        retf
        
;
;used to load the gdtr
;       void load_gdtr(void * addr);
;       
        align           16
        global          _load_gdtr
_load_gdtr:
        push    ebp
        mov     ebp, esp
        push    ebx
        mov     ebx, [ebp + 8]
        lgdt    [ebx]
        pop     ebx
        leave
        retn
;
;used to store the gdtr
;       void store_gdtr(void * sAddr);
;       
        align           16
        global          _store_gdtr
_store_gdtr:
        push    ebp
        mov     ebp, esp
        push    ebx
        mov     ebx, [ebp + 8]
        sgdt    [ebx]
        pop     ebx
        leave
        retn
